package com._21cn.cms.common.utils;

import com._21cn.framework.utils.JsonEncoder;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PropertyPreFilter;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 司徒海富 on 2015/9/7.
 */
public class ResponseUtil {

    public static void returnMsg(HttpServletResponse response, String content) {
        try {
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().write(content);
            response.getWriter().flush();
            response.getWriter().close();
        } catch(Exception e) {
        }
    }
    
    public static void returnJsonMsg(HttpServletResponse response, int ret, String msg, Map<String,Object> returnData){
		if( returnData==null ){
			returnData=new HashMap<String,Object>();
		}
		returnData.put("ret", ret);
		returnData.put("msg", msg);
		returnJsonMsg(response, createJson(returnData, "" ));
	}

	public static void returnJsonMsg(HttpServletResponse response, int ret, String msg,
                                     String keyName, Object obj, PropertyPreFilter filter){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("ret", ret);
		resultMap.put("msg", msg);
		resultMap.put(keyName, obj);
		String json = JSONObject.toJSONString(resultMap, filter);
		returnJsonMsg(response, json);
	}
	
	public static void returnJsonMsg(HttpServletResponse response, String json) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			response.getWriter().write(json);
			response.getWriter().flush();
			response.getWriter().close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void returnJsonMsg(HttpServletResponse response, Object obj, String jsonCallBackName) {
		String json = createJson(obj, jsonCallBackName);
		returnJsonMsg(response, json);
	}
	
	public static String createJson(Object obj, String jsonCallBackName){
		String json = JsonEncoder.encode(obj);
		if (!StringUtils.isEmpty(jsonCallBackName)) {
			json = jsonCallBackName + "(" + json + ")";
		}
		
		return json;
 	}

	/**
	 * dwz 处理ajax成功响应
	 * @param response
	 * @param message
	 * @param forwardUrl
	 */
	public static void returnAjaxSuccess(HttpServletResponse response, String message, String forwardUrl, String navTabId){
		Map<String, Object> resultMap = new HashMap();
		resultMap.put("statusCode",1);
		resultMap.put("message",message);
		resultMap.put("navTabId",navTabId);
		resultMap.put("rel",forwardUrl);
		resultMap.put("callbackType","");
		resultMap.put("forwardUrl",forwardUrl);
		String json = JsonEncoder.encode(resultMap);
		returnMsg(response, json);
	}
	
	
	public static void returnSuccessClose(HttpServletResponse response, String message, String forwardUrl, String navTabId){
		Map<String, Object> resultMap = new HashMap();
		resultMap.put("statusCode",1);
		resultMap.put("message",message);
		resultMap.put("navTabId",navTabId);
		resultMap.put("rel",forwardUrl);
		resultMap.put("callbackType","closeCurrent");
		resultMap.put("forwardUrl",forwardUrl);
		String json = JsonEncoder.encode(resultMap);
		returnMsg(response, json);
	}

	/**
	 * dwz 处理ajax失败响应
	 * @param response
	 * @param message
	 * @param forwardUrl
	 */
	public static void returnAjaxFail(HttpServletResponse response, String message, String forwardUrl, String navTabId){
		Map<String, Object> resultMap = new HashMap();
		resultMap.put("statusCode",0);
		resultMap.put("message",message);
		resultMap.put("navTabId",navTabId);
		resultMap.put("rel","");
		resultMap.put("callbackType","closeCurrent");
		resultMap.put("forwardUrl",forwardUrl);
		String json = JsonEncoder.encode(resultMap);
		returnMsg(response, json);
	}
	
	public static void returnAjaxFail(HttpServletResponse response, String message, String forwardUrl, String navTabId, String rel, String callbackType){
		Map<String, Object> resultMap = new HashMap();
		resultMap.put("statusCode",0);
		resultMap.put("message",message);
		resultMap.put("navTabId",navTabId);
		resultMap.put("rel",rel);
		resultMap.put("callbackType",callbackType);
		resultMap.put("forwardUrl",forwardUrl);
		String json = JsonEncoder.encode(resultMap);
		returnMsg(response, json);
	}
}
